package org.jivesoftware.smackx.filetransfer;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.OrFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.StreamInitiation;

public class FaultTolerantNegotiator extends StreamNegotiator
{
  private Connection connection;
  private PacketFilter primaryFilter;
  private StreamNegotiator primaryNegotiator;
  private PacketFilter secondaryFilter;
  private StreamNegotiator secondaryNegotiator;

  public FaultTolerantNegotiator(Connection paramConnection, StreamNegotiator paramStreamNegotiator1, StreamNegotiator paramStreamNegotiator2)
  {
    this.primaryNegotiator = paramStreamNegotiator1;
    this.secondaryNegotiator = paramStreamNegotiator2;
    this.connection = paramConnection;
  }

  private StreamNegotiator determineNegotiator(Packet paramPacket)
  {
    StreamNegotiator localStreamNegotiator;
    if (this.primaryFilter.accept(paramPacket))
      localStreamNegotiator = this.primaryNegotiator;
    while (true)
    {
      return localStreamNegotiator;
      localStreamNegotiator = this.secondaryNegotiator;
    }
  }

  public void cleanup()
  {
  }

  // ERROR //
  public InputStream createIncomingStream(StreamInitiation paramStreamInitiation)
    throws XMPPException
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 26	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator:connection	Lorg/jivesoftware/smack/Connection;
    //   4: aload_0
    //   5: aload_1
    //   6: invokevirtual 55	org/jivesoftware/smackx/packet/StreamInitiation:getFrom	()Ljava/lang/String;
    //   9: aload_1
    //   10: invokevirtual 58	org/jivesoftware/smackx/packet/StreamInitiation:getSessionID	()Ljava/lang/String;
    //   13: invokevirtual 62	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator:getInitiationPacketFilter	(Ljava/lang/String;Ljava/lang/String;)Lorg/jivesoftware/smack/filter/PacketFilter;
    //   16: invokevirtual 68	org/jivesoftware/smack/Connection:createPacketCollector	(Lorg/jivesoftware/smack/filter/PacketFilter;)Lorg/jivesoftware/smack/PacketCollector;
    //   19: astore_2
    //   20: aload_0
    //   21: getfield 26	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator:connection	Lorg/jivesoftware/smack/Connection;
    //   24: aload_0
    //   25: aload_1
    //   26: aload_0
    //   27: invokevirtual 72	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator:getNamespaces	()[Ljava/lang/String;
    //   30: invokespecial 76	org/jivesoftware/smackx/filetransfer/StreamNegotiator:createInitiationAccept	(Lorg/jivesoftware/smackx/packet/StreamInitiation;[Ljava/lang/String;)Lorg/jivesoftware/smackx/packet/StreamInitiation;
    //   33: invokevirtual 80	org/jivesoftware/smack/Connection:sendPacket	(Lorg/jivesoftware/smack/packet/Packet;)V
    //   36: new 82	java/util/concurrent/ExecutorCompletionService
    //   39: dup
    //   40: iconst_2
    //   41: invokestatic 88	java/util/concurrent/Executors:newFixedThreadPool	(I)Ljava/util/concurrent/ExecutorService;
    //   44: invokespecial 91	java/util/concurrent/ExecutorCompletionService:<init>	(Ljava/util/concurrent/Executor;)V
    //   47: astore_3
    //   48: new 93	java/util/ArrayList
    //   51: dup
    //   52: invokespecial 94	java/util/ArrayList:<init>	()V
    //   55: astore 4
    //   57: aconst_null
    //   58: astore 5
    //   60: aload 4
    //   62: aload_3
    //   63: new 6	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator$NegotiatorService
    //   66: dup
    //   67: aload_0
    //   68: aload_2
    //   69: invokespecial 97	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator$NegotiatorService:<init>	(Lorg/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator;Lorg/jivesoftware/smack/PacketCollector;)V
    //   72: invokeinterface 103 2 0
    //   77: invokeinterface 109 2 0
    //   82: pop
    //   83: aload 4
    //   85: aload_3
    //   86: new 6	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator$NegotiatorService
    //   89: dup
    //   90: aload_0
    //   91: aload_2
    //   92: invokespecial 97	org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator$NegotiatorService:<init>	(Lorg/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator;Lorg/jivesoftware/smack/PacketCollector;)V
    //   95: invokeinterface 103 2 0
    //   100: invokeinterface 109 2 0
    //   105: pop
    //   106: iconst_0
    //   107: istore 11
    //   109: aconst_null
    //   110: astore 12
    //   112: aload 5
    //   114: ifnonnull +19 -> 133
    //   117: aload 4
    //   119: invokeinterface 113 1 0
    //   124: istore 16
    //   126: iload 11
    //   128: iload 16
    //   130: if_icmplt +39 -> 169
    //   133: aload 4
    //   135: invokeinterface 117 1 0
    //   140: astore 13
    //   142: aload 13
    //   144: invokeinterface 123 1 0
    //   149: ifne +133 -> 282
    //   152: aload_2
    //   153: invokevirtual 128	org/jivesoftware/smack/PacketCollector:cancel	()V
    //   156: aload 5
    //   158: ifnonnull +154 -> 312
    //   161: aload 12
    //   163: ifnull +139 -> 302
    //   166: aload 12
    //   168: athrow
    //   169: iinc 11 1
    //   172: aload_3
    //   173: ldc2_w 129
    //   176: getstatic 136	java/util/concurrent/TimeUnit:SECONDS	Ljava/util/concurrent/TimeUnit;
    //   179: invokeinterface 140 4 0
    //   184: astore 18
    //   186: aload 18
    //   188: ifnull -76 -> 112
    //   191: aload 18
    //   193: invokeinterface 146 1 0
    //   198: checkcast 148	java/io/InputStream
    //   201: astore 5
    //   203: goto -91 -> 112
    //   206: astore 17
    //   208: goto -96 -> 112
    //   211: astore 20
    //   213: new 45	XMPPException
    //   216: dup
    //   217: aload 20
    //   219: invokevirtual 152	java/util/concurrent/ExecutionException:getCause	()Ljava/lang/Throwable;
    //   222: invokespecial 155	XMPPException:<init>	(Ljava/lang/Throwable;)V
    //   225: astore 21
    //   227: aload 21
    //   229: astore 12
    //   231: goto -119 -> 112
    //   234: astore 6
    //   236: aload 4
    //   238: invokeinterface 117 1 0
    //   243: astore 7
    //   245: aload 7
    //   247: invokeinterface 123 1 0
    //   252: ifne +10 -> 262
    //   255: aload_2
    //   256: invokevirtual 128	org/jivesoftware/smack/PacketCollector:cancel	()V
    //   259: aload 6
    //   261: athrow
    //   262: aload 7
    //   264: invokeinterface 158 1 0
    //   269: checkcast 142	java/util/concurrent/Future
    //   272: iconst_1
    //   273: invokeinterface 161 2 0
    //   278: pop
    //   279: goto -34 -> 245
    //   282: aload 13
    //   284: invokeinterface 158 1 0
    //   289: checkcast 142	java/util/concurrent/Future
    //   292: iconst_1
    //   293: invokeinterface 161 2 0
    //   298: pop
    //   299: goto -157 -> 142
    //   302: new 45	XMPPException
    //   305: dup
    //   306: ldc 163
    //   308: invokespecial 166	XMPPException:<init>	(Ljava/lang/String;)V
    //   311: athrow
    //   312: aload 5
    //   314: areturn
    //   315: astore 6
    //   317: aload 12
    //   319: pop
    //   320: goto -84 -> 236
    //   323: astore 19
    //   325: goto -213 -> 112
    //
    // Exception table:
    //   from	to	target	type
    //   172	186	206	java/lang/InterruptedException
    //   191	203	211	java/util/concurrent/ExecutionException
    //   60	106	234	finally
    //   117	126	315	finally
    //   172	186	315	finally
    //   191	203	315	finally
    //   213	227	315	finally
    //   191	203	323	java/lang/InterruptedException
  }

  public OutputStream createOutgoingStream(String paramString1, String paramString2, String paramString3)
    throws XMPPException
  {
    OutputStream localOutputStream1;
    OutputStream localOutputStream2;
    try
    {
      localOutputStream2 = this.primaryNegotiator.createOutgoingStream(paramString1, paramString2, paramString3);
      localOutputStream1 = localOutputStream2;
      return localOutputStream1;
    }
    catch (XMPPException localXMPPException)
    {
      localOutputStream1 = this.secondaryNegotiator.createOutgoingStream(paramString1, paramString2, paramString3);
    }
  }

  public PacketFilter getInitiationPacketFilter(String paramString1, String paramString2)
  {
    if ((this.primaryFilter == null) || (this.secondaryFilter == null))
    {
      this.primaryFilter = this.primaryNegotiator.getInitiationPacketFilter(paramString1, paramString2);
      this.secondaryFilter = this.secondaryNegotiator.getInitiationPacketFilter(paramString1, paramString2);
    }
    return new OrFilter(this.primaryFilter, this.secondaryFilter);
  }

  public String[] getNamespaces()
  {
    String[] arrayOfString1 = this.primaryNegotiator.getNamespaces();
    String[] arrayOfString2 = this.secondaryNegotiator.getNamespaces();
    String[] arrayOfString3 = new String[arrayOfString1.length + arrayOfString2.length];
    System.arraycopy(arrayOfString1, 0, arrayOfString3, 0, arrayOfString1.length);
    System.arraycopy(arrayOfString2, 0, arrayOfString3, arrayOfString1.length, arrayOfString2.length);
    return arrayOfString3;
  }

  final Packet initiateIncomingStream(Connection paramConnection, StreamInitiation paramStreamInitiation)
  {
    throw new UnsupportedOperationException("Initiation handled by createIncomingStream method");
  }

  InputStream negotiateIncomingStream(Packet paramPacket)
    throws XMPPException
  {
    throw new UnsupportedOperationException("Negotiation only handled by create incoming stream method.");
  }

  private class NegotiatorService
  implements Callable<InputStream>
  {
    private PacketCollector collector;

    NegotiatorService(, PacketCollector paramPacketCollector)
    {
      this.collector = paramPacketCollector;
    }

    public InputStream call()
      throws Exception
    {
      Packet localPacket = this.collector.nextResult(2 * SmackConfiguration.getPacketReplyTimeout());
      if (localPacket == null)
        throw new XMPPException("No response from remote client");
      return FaultTolerantNegotiator.access$0(this.this$0, localPacket).negotiateIncomingStream(localPacket);
    }
  }
}